package com.mlnx.sim.ecg;


import com.mlnx.mptp.mptp.MpPacket;
import com.mlnx.sim.ecg.client.MpClientBootstrap;
import com.mlnx.sim.ecg.client.MpClientLis;
import com.mlnx.sim.ecg.config.Config;
import com.mlnx.tp.base.model.DeviceType;

/**
 * @author fzh
 * @create 2018/3/26 16:57
 */
public class MpClient {

    private MpClientBootstrap mpClientBootstrap;
    private MpClientLis mpClientLis;

    public MpClient(MpClientLis mpClientLis) {
        this.mpClientLis = mpClientLis;
        this.mpClientBootstrap = new MpClientBootstrap(Config.MP_DEVICE_PORT, Config.HOST, mpClientLis);
    }

    public void register(final String deviceId) {
        MpPacket mpPacket = new MpPacket();
        mpPacket.register(deviceId, DeviceType.ECG_DEVICE);
        try {
            mpClientBootstrap.send(mpPacket);
        } catch (Exception e) {
            e.printStackTrace();
            mpClientLis.sendError();
        }
    }

    public void pong() {
        MpPacket mpPacket = new MpPacket();
        mpPacket.pong(DeviceType.ECG_DEVICE);
        try {
            mpClientBootstrap.send(mpPacket);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void disConnect() {
        mpClientBootstrap.socketChannel.close();
    }

    public void push(MpPacket packet) {
        try {
            mpClientBootstrap.send(packet);
        } catch (Exception e) {
            e.printStackTrace();
            mpClientLis.sendError();
        }
    }
}
